import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

//网络请求
import { ConfigProvider } from '../../providers/config/config';
import { HttpserviesProvider } from '../../providers/httpservies/httpservies';
import { Http,Jsonp } from '@angular/http';

/**
 * Generated class for the ProductlistPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-productlist',
  templateUrl: 'productlist.html',
})
export class ProductlistPage {

  public list = [];
  public product_cid = '';
  public page = 1;

  constructor(public navCtrl: NavController, public navParams: NavParams, public config: ConfigProvider, public httpServices: HttpserviesProvider) {

    this.product_cid = this.navParams.get('cid');
    console.log(this.navParams.get('cid'));

    //请求商品列表数据
    this.requestProductListData('');

  }

  ionViewDidLoad() {
    // console.log('ionViewDidLoad ProductlistPage');
  }

  //请求商品列表数据
  requestProductListData(ionInfinite){
    var apiurl = 'api/plist?cid='+this.product_cid+'&page='+this.page;
    this.httpServices.requestData(apiurl,(data)=>{
      console.log(data.result);
      this.list = this.list.concat(data.result);//数据拼接
      if(ionInfinite){//告诉ionic数据请求完成
        ionInfinite.complete();
        if(data.result.length<10){
          //请求到最后一页，已经加载完全部的数据
          ionInfinite.enable(false);
          console.log('请求到最后一页，已经加载完全部的数据');
        };
        this.page++; //页码+1;
      }
    });
  }

  //上拉加载更多
  doMoreLoadData(ionInfinite){
    this.requestProductListData(ionInfinite);
  }

}
